package com.qizhou.compiler.enitity.func

import com.qizhou.compiler.FetchStrategyClassName
import com.qizhou.compiler.FilterHttpCodeClassType
import com.qizhou.compiler.SimpleDataSourceClassType
import com.qizhou.compiler.SpCacheHttpClassName
import com.qizhou.compiler.aptUtils.logger.Logger
import com.qizhou.compiler.aptUtils.types.toClassName
import com.qizhou.compiler.enitity.FuncBuilder
import com.squareup.kotlinpoet.FunSpec

class AutoApiWithCacheFunc (private val mReposityMothed : AutoApiWithCacheMethod)  : FuncBuilder(mReposityMothed) {


    override fun addStatement(funcBuilder: FunSpec.Builder) {
        val paramsStringBuilder = StringBuilder()
        reposityMothed.parameters.forEach {
            paramsStringBuilder.append(it.name).append(",")
        }
        Logger.warn("addStatement.addStatement  SpCacheFuncSpCacheFunc")

        if (reposityMothed.filterFunClass!=null) {
            funcBuilder.addStatement(" return %T(%T(),%T.%L){\n " +
                    "apiService.%L(%L)"
                    + ".%T()"
                    + "\n}.startFetchData()"
                    , SimpleDataSourceClassType
                    ,  mReposityMothed.provideClass!!

                    , FetchStrategyClassName
                    , mReposityMothed.fetchStrategy
                    , reposityMothed.methodName
                    , paramsStringBuilder.toString().dropLast(1)
                    , reposityMothed.filterFunClass!!)
        } else {
            funcBuilder.addStatement(" return %T(%T(%S,%L),%T.%L){\n " +
                    "apiService.%L(%L)"
                    + "\n}.startFetchData()"
                    , SimpleDataSourceClassType
                    , mReposityMothed.provideClass!!
                    , FetchStrategyClassName
                    , mReposityMothed.fetchStrategy
                    , reposityMothed.methodName
                    , paramsStringBuilder.toString().dropLast(1))
        }
    }

}